﻿using log4net;
using log4net.Config;
using System;
using System.IO;
using System.Reflection;

namespace Common.Utility
{
    /// <summary>
    /// 直接使用
    /// </summary>
    public class LogHelper
    {

        private static readonly ILog logInfo = LogManager.GetLogger("loginfo");
        static LogHelper()
        {
            var logRepo = LogManager.CreateRepository(Assembly.GetEntryAssembly(), typeof(log4net.Repository.Hierarchy.Hierarchy));
            XmlConfigurator.Configure(logRepo, new FileInfo("log4net.config"));

            log4net.Config.XmlConfigurator.Configure();
        }

        /// <summary>
        /// 记录错误日志
        /// </summary>
        /// <param name="text">消息内容</param>
        /// <param name="ex">异常信息</param>
        public static void Error(string text, Exception ex = null)
        {
            logInfo.Error(text, ex);
        }

        public static void Exception(Exception ex, string description = "")
        {
            logInfo.Error(description, ex);
        }

        /// <summary>
        /// 记录严重错误日志
        /// </summary>
        /// <param name="message">消息内容</param>
        /// <param name="ex">异常信息</param>
        /// <param name="isWriteDebug">是否在output输出</param>
        public static void Fatal(string message, Exception ex = null)
        {
            logInfo.Fatal(message, ex); 
        }


        /// <summary>
        /// 记录警告日志
        /// </summary>
        /// <param name="message">消息内容</param>
        /// <param name="ex">异常信息</param>
        /// <param name="isWriteDebug">是否在output输出</param>
        public static void Warn(string message, Exception ex = null)
        {
            logInfo.Warn(message, ex);
        }

        /// <summary>
        /// 记录信息日志
        /// </summary>
        /// <param name="message">消息内容</param>
        /// <param name="ex">异常信息</param>
        /// <param name="isWriteDebug">是否在output输出</param>
        public static void Info(string message, Exception ex = null)
        {
            logInfo.Info(message, ex);
        }

        /// <summary>
        /// 记录调试日志
        /// </summary>
        /// <param name="message">消息内容</param>
        public static void Debug(string message)
        {
            logInfo.Debug(message);
        }


    }


    /* 

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>
    <section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
  </configSections>
  <log4net>
    <root>
      <priority value="ALL" />
      <appender-ref ref="RollingLogFileAppender" />
      <appender-ref ref="RollingLogWarnFileAppender"/>
      <appender-ref ref="RollingLogFatalFileAppender"/>
      <appender-ref ref="RollingLogInfoFileAppender"/>
      <!--<appender-ref ref="ColoredConsoleAppender" />-->
    </root>
    <appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
      <param name="File" value="Log\\Error\\" />
      <param name="AppendToFile" value="true" />
      <param name="MaxFileSize" value="10240" />
      <param name="MaxSizeRollBackups" value="100" />
      <param name="StaticLogFileName" value="false" />
      <param name="DatePattern" value="yyyy_MM_dd'_err.log'" />
      <param name="RollingStyle" value="Date" />
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%date %-5level %message%newline" />
      </layout>
      <filter type="log4net.Filter.LevelRangeFilter">
        <levelMin value="ERROR" />
        <levelMax value="ERROR" />
      </filter>
    </appender>
    <appender name="RollingLogWarnFileAppender" type="log4net.Appender.RollingFileAppender">
      <param name="File" value="Log\\Warn\\" />
      <param name="AppendToFile" value="true" />
      <param name="MaxFileSize" value="10240" />
      <param name="MaxSizeRollBackups" value="100" />
      <param name="StaticLogFileName" value="false" />
      <param name="DatePattern" value="yyyy_MM_dd'_warn.log'" />
      <param name="RollingStyle" value="Date" />
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%date %-5level %message%newline" />
      </layout>
      <filter type="log4net.Filter.LevelRangeFilter">
        <levelMin value="WARN" />
        <levelMax value="WARN" />
      </filter>
    </appender>
    <appender name="RollingLogFatalFileAppender" type="log4net.Appender.RollingFileAppender">
      <param name="File" value="Log\\Fatal\\" />
      <param name="AppendToFile" value="true" />
      <param name="MaxFileSize" value="10240" />
      <param name="MaxSizeRollBackups" value="100" />
      <param name="StaticLogFileName" value="false" />
      <param name="DatePattern" value="yyyy_MM_dd'_fatal.log'" />
      <param name="RollingStyle" value="Date" />
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%date %-5level %message%newline" />
      </layout>
      <filter type="log4net.Filter.LevelRangeFilter">
        <levelMin value="FATAL" />
        <levelMax value="FATAL" />
      </filter>
    </appender>
    <appender name="RollingLogInfoFileAppender" type="log4net.Appender.RollingFileAppender">
      <param name="File" value="Log\\Info\\" />
      <param name="AppendToFile" value="true" />
      <param name="MaxFileSize" value="10240" />
      <param name="MaxSizeRollBackups" value="100" />
      <param name="StaticLogFileName" value="false" />
      <param name="DatePattern" value="yyyy_MM_dd'_info.log'" />
      <param name="RollingStyle" value="Date" />
      <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%date %-5level %message%newline" />
      </layout>
      <filter type="log4net.Filter.LevelRangeFilter">
        <levelMin value="INFO" />
        <levelMax value="INFO" />
      </filter>
    </appender>
  </log4net>

</configuration>

     
     
     */

}
